
#include "PauseState.h"

PauseState::PauseState(): 
	mCameraMan(nullptr),
	running(false)
{}

void PauseState::Enter(int prevState)
{
	createScene();
	GameServices::InputManager()->AddListener(this);
	previous_state = prevState;
}

bool PauseState::IsRunning()
{
	return running;
}

void PauseState::Pause()
{
	//Not used because this is pause
}

void PauseState::Unpause()
{
	//Not used because this is pause
}

void PauseState::Execute()
{
}

void PauseState::Exit()
{
	Ogre::String name;

	//Destroy robot entity
	GameServices::OgreManager()->GetSceneManager()->destroyEntity("pause0");

	//Destroy the robot scene node
	GameServices::OgreManager()->GetSceneManager()->destroySceneNode("pause0");

	//Destroy the scene nodes & entites for all other objects
	for(int i = 0; i < PAUSE_LAYOUT_SIZE; i++)
	{
		if(bitmap[i])
		{
			name =  "pause" + Ogre::StringConverter::toString(i+1);
			GameServices::OgreManager()->GetSceneManager()->destroyEntity(name);
			GameServices::OgreManager()->GetSceneManager()->destroySceneNode(name);
		}
	}
	Cleanup();
}

void PauseState::Initialize()
{
	//Read in txt file indicating where to place objects
	std::ifstream inFile;
	char ch;				//Stores characters read in from file
	char compare = '1';		//Compare read in chararcter to
	int i = 0;				//Looping var

	inFile.open("../../res/Layout_Pause.txt");

	if(!inFile)
	{
		std::cout<<"Error reading in layout file";
	}
	else
	{
		while(!inFile.eof() && i < PAUSE_LAYOUT_SIZE)
		{
			inFile.get(ch);
			if(ch == compare)
			{
				bitmap[i] = true;	//place block by grid position
			}
			else
			{
				bitmap[i] = false;	//else don't place a block
			}

			i++;
		}
	}

	inFile.close();
	mCameraMan = new OgreBites::SdkCameraMan(GameServices::OgreManager()->GetCamera());
	mCameraMan->setStyle(OgreBites::CS_ORBIT);
}

void PauseState::UpdateStarted(const Ogre::FrameEvent& gameTime)
{
	mCameraMan->frameRenderingQueued(gameTime);
}

void PauseState::UpdateEnded(const Ogre::FrameEvent& gameTime)
{

}

void PauseState::Cleanup()
{
	GameServices::InputManager()->RemoveListener(this);
}

void PauseState::createScene()
{
	int i = 0, row_count, col_count;
	Ogre::String name;

	int x_step =  50;
	int y_step =  50;
	int windowHeight = GameServices::OgreManager()->GetRenderWindow()->getHeight();
	int windowWidth = GameServices::OgreManager()->GetRenderWindow()->getWidth();

	int pos_x, pos_y;	//An offset to ensure the Pause blocks to not over lap other
						//block titles

	//Create a robot
	Ogre::SceneNode *pauseSceneNode(GameServices::OgreManager()->GetSceneManager()->getRootSceneNode()->createChildSceneNode("pause0"));
	Ogre::Entity *pauseSphere(GameServices::OgreManager()->GetSceneManager()->createEntity("pause0" , "robot.mesh"));
	pauseSceneNode->attachObject(pauseSphere);
	pauseSceneNode->setScale(1, 10, 10);
	pauseSceneNode->yaw(Ogre::Radian(Ogre::Degree(-90)));
	pauseSceneNode->setPosition(PAUSE_DISTANCE, PAUSE_DISTANCE, 10);

	//Place objects on the screen based on the bit map
	for(row_count = 0; row_count < PAUSE_LAYOUT_ROWS; row_count++)
	{
		for(col_count = 0; col_count < PAUSE_LAYOUT_COLS; col_count++)
		{
			if(bitmap[i])
			{
				//place an object at point on screen
				pos_x = (-7000 + (col_count * x_step));	//Calculate x position
				pos_y = PAUSE_DISTANCE + (row_count * y_step);	//Calculate y position
				pos_y = pos_y - (2 * pos_y);					//Invert it to have the words rightside up
				name = "pause" + Ogre::StringConverter::toString(i+1);
				pauseSceneNode = GameServices::OgreManager()->GetSceneManager()->getRootSceneNode()->createChildSceneNode(name);
				pauseSphere = GameServices::OgreManager()->GetSceneManager()->createEntity(name , "cube.mesh");
				pauseSceneNode->attachObject(pauseSphere);
				pauseSceneNode->setScale(.3,.3,.3);
				pauseSceneNode->setPosition(pos_x, pos_y , 1);
			}

			i++;
		}
	}
}

void PauseState::OnKeyDown(const OIS::KeyCode& key)
{
	switch(key)
	{
	case OIS::KC_ESCAPE:
		GameServices::GameEngine()->ShutDown();
		break;
	case OIS::KC_P:
		this->Exit();
		GameServices::StateManager()->ChangeState(previous_state, PAUSE, true);
		break;
	}
	//mCameraMan->injectKeyDown(key); //notify the camera man object of a key event

}

void PauseState::OnKeyUp(const OIS::KeyCode& key)
{
	//mCameraMan->injectKeyUp(key); //notify the camera man object of a key event
}

void PauseState::OnKeyHeld(const OIS::KeyCode& key)
{}

void PauseState::OnMouseDown(const OIS::MouseState& mouse, const OIS::MouseButtonID& id)
{
	//mCameraMan->injectMouseDown(mouse, id); //notify the camera man object of a key event
}

void PauseState::OnMouseUp(const OIS::MouseState& mouse, const OIS::MouseButtonID& id)
{
	//mCameraMan->injectMouseUp(mouse, id); //notify the camera man object of a key event
}

void PauseState::OnMouseHeld(const OIS::MouseState& mouse, const OIS::MouseButtonID& id)
{}

void PauseState::OnMouseMoved(const OIS::MouseState& mouse)
{
	//mCameraMan->injectMouseMove(mouse); //notify the camera man object of a key event
}


